package com.yunxi.controller.sys;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.PosVO;
import com.yunxi.sys.service.PosDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 组织结构 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-08
 */
@RestController
@RequestMapping("/admin/pos")
@Api(tags = "sys:pos", description = "组织结构接口")
public class PosController {
    private static final Logger logger = LoggerFactory.getLogger(PosController.class);

    @Autowired
    PosDubboService posDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
                    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页数量", required = true, dataType = "int")})
    public ApiResult page(PosVO orgVO, Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(posDubboService.findPageByVO(orgVO, pageNum, pageSize));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", name = "orgVO", value = "岗位对象", required = false, dataType = "PosVO"),
            }
    )
    public ApiResult list(PosVO posVO) throws Exception {
        return ResponseUtil.ok(posDubboService.listByVo(posVO));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    public ApiResult load(String positionId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(posDubboService.getById(positionId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody PosVO orgVo, @CurrentUser LoginInfo loginInfo) throws Exception {

        return posDubboService.saveOrUpdate(orgVo,loginInfo);
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<PosVO> orgVoList) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        posDubboService.removeByListVO(orgVoList);
        return ResponseUtil.ok("success");
    }
}
